#include "../include/TcpClient.h"
#include "../include/Configuration.h"
#include "../include/ProtocolParser.h"
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

TcpClient::TcpClient(Configuration& conf)
:_tcpCon(connection(conf))
{

}

TcpClient::~TcpClient()
{

}

int TcpClient::connection(Configuration &conf){
    string ip=conf.getConfMap()["ip"];
    unsigned short port=atoi(conf.getConfMap()["port"].c_str());
    Connection conn(ip,port);
    return conn.fd();
}

void TcpClient::start(){
    int messageId=1;//默认keyword search
    string str;

    while(1){
        cout<<"*****************************"<<endl;
        cout<<"*     Enter a word          *"<<endl;
        cout<<"*     'q': quit             *"<<endl;
        cout<<"*     '1': keyword search   *"<<endl;
        cout<<"*     '2': web search       *"<<endl;
        cout<<"*****************************"<<endl<<endl;

        if (messageId == 1) {
            cout<<"*****************************"<<endl;
            cout<<"*      keyword search       *"<<endl;
            cout<<"*****************************"<<endl;

            while(1) {
                cout << ">>>> ";
                getline(cin, str);

                // 移除前后空格
                str.erase(0, str.find_first_not_of(" \t\r\n"));
                str.erase(str.find_last_not_of(" \t\r\n") + 1);

                if (str.empty()) {
                    continue;
                } else if (str == "q") {
                    return;
                } else if (str == "1") {
                    messageId = 1;
                    break;
                } else if (str == "2") {
                    messageId = 2;
                    break;
                } else {
                    ProtocolParser pp;
                    string message=pp.jsonMsgPack("1",str);
                    _tcpCon.send(message);
                    message=_tcpCon.receive();
                    pp.handleMessage(message);
                }
            }
        }

        if (messageId == 2) {
            cout<<"*****************************"<<endl;
            cout<<"*        web search         *"<<endl;
            cout<<"*****************************"<<endl;
            while(1) {
                cout << ">>>> ";
                getline(cin, str);

                str.erase(0, str.find_first_not_of(" \t\r\n"));
                str.erase(str.find_last_not_of(" \t\r\n") + 1);

                if (str.empty()) {
                    continue;
                } else if (str == "q") {
                    return;
                } else if (str == "1") {
                    messageId = 1;
                    break;
                } else if (str == "2") {
                    messageId = 2;
                    break;
                } else {
                    ProtocolParser pp;
                    string message=pp.jsonMsgPack("2",str);
                    _tcpCon.send(message);
                    message=_tcpCon.receive();
                    pp.handleMessage(message);
                }
            }
        }
    }
}
